package com.tosee.dao.dbTosee.impl;

import java.sql.SQLException;

import org.apache.commons.lang.StringUtils;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import com.tosee.dao.dbTosee.IUserDao;
import com.tosee.dao.hibernate.impl.HibernateGenericDao;
import com.tosee.model.dbTosee.User;

@Repository
public class UserDao extends HibernateGenericDao<User, Long> implements IUserDao {

    /**
     * Constructeur du DAO.
     * 
     * @param sessionFactory {@link SessionFactory} Hibernate
     */
    @Autowired
    public UserDao(SessionFactory sessionFactory) {
        super(User.class, sessionFactory);
    }

    /**
     * {@inheritDoc}
     */
    public User getUserByLogin(final String _login) {
        User user = null;
        if(StringUtils.isNotBlank(_login)) {
            user = (User) getHibernateTemplate().execute(new HibernateCallback() {

                @Override
                public Object doInHibernate(Session _session) throws HibernateException, SQLException {

                    // Construction de la requete
                    StringBuilder requeteBuilder = new StringBuilder(200);
                    requeteBuilder.append("SELECT user ");
                    requeteBuilder.append("FROM User user ");

                    requeteBuilder.append("LEFT JOIN FETCH user.listeSerie serie ");
                    requeteBuilder.append("LEFT JOIN FETCH user.listeEpisode episode ");
                    requeteBuilder.append("WHERE user.login = :login ");

                    // Exécution de la requete
                    Query requete = _session.createQuery(requeteBuilder.toString());
                    requete.setString("login", _login);

                    return (User) requete.uniqueResult();
                }
            });
        }
        return user;
    }

    @Override
    public User getUserById(final Integer _id) {
        User user = null;
        if(_id != null) {
            user = (User) getHibernateTemplate().execute(new HibernateCallback() {

                @Override
                public Object doInHibernate(Session _session) throws HibernateException, SQLException {

                    // Construction de la requete
                    StringBuilder requeteBuilder = new StringBuilder(200);
                    requeteBuilder.append("SELECT user ");
                    requeteBuilder.append("FROM User user ");
                    requeteBuilder.append("LEFT JOIN FETCH user.listeSerie serie ");
                    requeteBuilder.append("LEFT JOIN FETCH user.listeEpisode episode ");
                    requeteBuilder.append("WHERE user.id = :id ");

                    // Exécution de la requete
                    Query requete = _session.createQuery(requeteBuilder.toString());
                    requete.setInteger("id", _id);

                    return (User) requete.uniqueResult();
                }
            });
        }
        return user;
    }

}
